﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuanAnhPersonalWeb.Models;



namespace TuanAnhPersonalWeb.Classes
{
    public class ImageAccessConsolidator
    {
        TuanAnhEntities db = new TuanAnhEntities();
        public IEnumerable<TUANANH__ALBUM> getAllAlbum()
        {
            var data = db.TUANANH__ALBUM.Select(e => e).ToList();
            return data.Count() > 0 ? data : null;
        }
        public IList<ImageModel> getAllImage()
        {
            IList<ImageModel> list = new List<ImageModel>();
            var imageList = db.TUANANH__IMAGE.ToList();
            foreach (var item in imageList)
            {
                ImageModel model = new ImageModel();
                
                model.IMAGE_ID = item.IMAGE_ID;
                model.IMAGE_DIR = item.IMAGE_DIR;
                model.IMAGE_THUMB = item.IMAGE_THUMB;
                model.IMAGE_DESCRIPTION = item.IMAGE_DESCRIPTION;
                model.CREATE_DATE = item.CREATE_DATE;
                model.ALBUM_ID = item.ALBUM_ID;
                model.ALBUM_NAME = db.TUANANH__ALBUM.Where(e => e.ALBUM_ID == item.ALBUM_ID).Select(e => e.ALBUM_TITLE).Single();
                list.Add(model);
            }
            return list;
        }
        public ImageModel getImagebyID(int id)
        {
            TUANANH__IMAGE item = db.TUANANH__IMAGE.Single(e => e.IMAGE_ID == id);
            ImageModel model = new ImageModel();
            model.IMAGE_ID = item.IMAGE_ID;
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.IMAGE_THUMB = item.IMAGE_THUMB;
            model.IMAGE_DESCRIPTION = item.IMAGE_DESCRIPTION;
            model.CREATE_DATE = item.CREATE_DATE;
            model.ALBUM_ID = item.ALBUM_ID;
            return model;
        }
        public void insertImage(ImageModel item)
        {
            TUANANH__IMAGE model = new TUANANH__IMAGE();
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.IMAGE_THUMB = item.IMAGE_THUMB;
            model.IMAGE_DESCRIPTION = item.IMAGE_DESCRIPTION;
            model.CREATE_DATE = DateTime.Now;
            model.ALBUM_ID = item.ALBUM_ID;
            db.TUANANH__IMAGE.AddObject(model);
            db.SaveChanges();
        }
        public void editImage(ImageModel item)
        {
            TUANANH__IMAGE model = db.TUANANH__IMAGE.Single(e => e.IMAGE_ID == item.IMAGE_ID);
            
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.IMAGE_THUMB = item.IMAGE_THUMB;
            model.IMAGE_DESCRIPTION = item.IMAGE_DESCRIPTION;
            model.CREATE_DATE = item.CREATE_DATE;
            model.ALBUM_ID = item.ALBUM_ID;
            db.SaveChanges();
        }
        public void deleteImageInAlbum(int id)
        {
            IList<TUANANH__IMAGE> list = db.TUANANH__IMAGE.Where(e => e.ALBUM_ID == id).ToList();
            if (list.Count() > 0)
            {
                foreach (var item in list)
                {
                    db.DeleteObject(item);
                    db.SaveChanges();
                }
            }

        }
    }
}